package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.admin.MarketKeywordService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiresAuthentication
@RestController
@RequestMapping("admin/keyword")
public class AdminKeywordController {
    // 写一个service
    @Autowired
    MarketKeywordService keywordService;

    @GetMapping("list")
    public Object list(Integer page, Integer limit, String sort, String order, String id, String keyword, String url,String isHot,String isDefault, String addTime, String updateTime,String sort_order ) {

        String sortOrder = sort_order;
        if (StringUtils.isEmpty(sort)){
            sort="add_time";
        }
        if (StringUtils.isEmpty(order)){
            order="desc";
        }
        // 调用keywordService
        List<MarketKeyword> marketKeywordList = keywordService.list(page,limit,id,sort,order,keyword,url,isHot,isDefault,addTime,updateTime,sortOrder);

        return ResponseUtil.okList(marketKeywordList);
    }


    @PostMapping("create")
    public Object create(@RequestBody MarketKeyword marketKeyword) {

        // 新增成员列表
        marketKeyword = keywordService.create(marketKeyword);
        return ResponseUtil.ok(marketKeyword);
    }


    @PostMapping("delete")
    public Object delete(@RequestBody Map<String,Integer> map) {


        Integer id = map.get("id");
        int row = keywordService.delete(id);

        if (row == 1) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }


    @PostMapping("update")
    public Object update(@RequestBody Map<String,Object> map ) {

        Integer id = (Integer) map.get("id");
        String keyword = (String) map.get("keyword");
        String url = (String) map.get("url");
        Boolean isHot = (Boolean) map.get("isHot");
        Boolean isDefault = (Boolean) map.get("isDefault");
        String addTime = (String) map.get("addTime");
        String updateTime = (String) map.get("updateTime");

        int row = keywordService.update(id,keyword,url,isHot,isDefault,addTime,updateTime);
        if (row==1){
          return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }
}